package com.example.bdmanagerapplication

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase


@Database(version = 3, entities = [PoiEntity::class],exportSchema = false)
abstract class AppDatabase : RoomDatabase() {

    abstract fun poiInfoDao(): PoiInfoDao
    companion object {
        val MIGRATION_2_3=object : Migration(0,1){
            override fun migrate(database: SupportSQLiteDatabase) {
                // 删除旧表（如果存在）
                database.execSQL("DROP TABLE IF EXISTS `PoiEntity`")
                database.execSQL("CREATE TABLE IF NOT EXISTS `PoiEntity` (`uid` STRING PRIMARY KEY  NOT NULL)")

            }
        }

        private var instance: AppDatabase? = null

        @Synchronized
        fun getDatabase(context: Context):AppDatabase{
            instance?.let {
                return it
            }
            return  Room.databaseBuilder(context.applicationContext,AppDatabase::class.java,"app_database")
                .addMigrations(MIGRATION_2_3)
                .fallbackToDestructiveMigration()
                .build().apply {
                    instance=this
                }
        }
    }
}